package com.xuzz.study.solr;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.PivotField;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.util.NamedList;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class SolrApplication {
    public static void main(String[] args)
    {
//        addDocument();
//        query();
//        mutliIndexQuery();
        try {
//            facetPivotQuery();

            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            System.out.println(sdf.format(sdf1.parse("2019-04-03 00:00:00")));
        }
        catch (Exception e)
        {

        }
    }


    public static void addDocument()
    {
        try {
            HttpSolrClient client = new HttpSolrClient("http://localhost:8983/solr/detect2");

            for (int i = 0; i < 100; i++) {
                SolrInputDocument sd = new SolrInputDocument();
                sd.addField("detectName", "detectName" + i);
                sd.addField("id", UUID.randomUUID().toString());
                sd.addField("srcIp", "192.168.1." + new Random().nextInt(200));
                sd.addField("dstIp", "10.2.1." + new Random().nextInt(200));
                sd.addField("dstPort", new Random().nextInt(1000));
                sd.addField("srcDev", "未知设备");
                sd.addField("dstSys", "mac os");
                sd.addField("datasource", "flume");
                sd.addField("detectTime", new Date());

                client.add(sd);
                client.commit();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void mutliIndexQuery()
    {
        try {
            HttpSolrClient client = new HttpSolrClient("http://localhost:8983/solr/detect");
            String shards ="http://localhost:8983/solr/detect2,http://localhost:8983/solr/detect2";
            ModifiableSolrParams  query = new ModifiableSolrParams();
//            query.set("shards.tolerant", true);
            query.set("shards",shards);
            query.set("q","*:*");
//            query.set("collection",shards);
            QueryResponse response = client.query(query);
            SolrDocumentList docs = response.getResults();
            System.out.println(docs.getNumFound());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void query()
    {
        try {
            HttpSolrClient client = new HttpSolrClient("http://localhost:8983/solr/detect");
            SolrQuery query = new SolrQuery();
            query.set("shards.tolerant", true);
            query.set("q","*:*");
            query.setFields(new String[]{"detectName","detectTime","srcIp","dstIp","dstPort"
                    ,"dstSys","srcDev","status"});

            QueryResponse response = client.query(query);
            SolrDocumentList docs = response.getResults();
            for(SolrDocument document:docs)
            {

                SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
                try{
                    Date logDate1 = sdf.parse(document.getFieldValue("detectTime").toString());
                    System.out.println(logDate1);
                    SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    System.out.println(sdf2.format(logDate1));
                }catch(Exception e){
                    System.out.println(e.getMessage());
                }

//                System.out.println(solrUTCDateToString(document.getFieldValue("detectTime").toString()));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    public static String solrUTCDateToString(String solrDate)
    {
       SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         TimeZone utcZone = TimeZone.getTimeZone("UTC");
         sf.setTimeZone(utcZone);
         Date date = null;
         String dateTime = "";
         try {
          date = sf.parse(solrDate);
          dateTime = sdf.format(date);
         } catch (ParseException e) {
          e.printStackTrace();
         }
         return dateTime;
    }

    public static void facetPivotQuery() throws Exception
    {
        SolrQuery query = new SolrQuery();
        query.setFacet(true);
        query.set("facet.pivot","srcIp,dstIp");
        query.set("facet.limit",10);
        query.set("facet.offset",0);
        query.set("q","srcIp:192.168.1.52");
        query.set("start",0);
        query.set("rows",0);
        HttpSolrClient client = new HttpSolrClient("http://localhost:8983/solr/detect");
        QueryResponse response = client.query(query);
        NamedList<List<PivotField>> namedList = response.getFacetPivot();

        List<PivotField> srcIpList = null;
        for(int i=0;i<namedList.size();i++){
            srcIpList = namedList.getVal(i);
            if(srcIpList != null){
                for(PivotField srcIp:srcIpList){
                    List<PivotField> dstIpList = srcIp.getPivot();
                    if(dstIpList != null){
                        for(PivotField dstIP:dstIpList){
                            System.out.println("srcIp:"+srcIp.getValue()+"dstIp:"+dstIP.getValue()+",c"+dstIP.getCount());
                        }
                        }
                    }

                }
            }

    }
}
